#!/usr/bin/env node

/**
 * 功能：构建Docker镜像
 * 使用：
 *  - ./scripts/bundle
 *  - ./scripts/bundle --proxy
 */

import { createRequire } from 'node:module'
import process from 'node:process'
import {
  OPEN_SOURCE_ADDRESS,
  buildImage,
  getRecentGitCommit,
} from '@142vip/utils'

(async () => {
  try {
    // 获取package.json文件
    const pkg = createRequire(import.meta.url)('../package.json')

    // 镜像地址
    const imageName = `${OPEN_SOURCE_ADDRESS.DOCKER_ALIYUNCS_VIP}/docs:${pkg.name}-${pkg.version}`

    // 最近一次提交信息
    const { hash: gitHash } = await getRecentGitCommit()

    // 构建镜像
    await buildImage({
      imageName,
      buildArgs: [
        // 参数中是否包含 --proxy
        ['NEED_PROXY', process.argv.includes('--proxy')],
        ['APP_NAME', pkg.name],
        ['APP_VERSION', pkg.version],
        ['APP_DESCRIPTION', pkg.description],
        ['AUTHOR', pkg.authorInfo.name],
        ['EMAIL', pkg.authorInfo.email],
        ['HOME_PAGE', pkg.authorInfo.homePage],
        ['GIT_HASH', gitHash],
      ],
      memory: 20000,
      push: true,
      delete: true,
      logger: true,
    })
  }
  catch (e) {
    console.log('异常信息:', e)
  }
})()
